
package tkatva.consulting.jmxpoller.smxService.impl

import tkatva.consulting.jmxconnection.beans.JmxBean
import tkatva.consulting.jmxconnection.beans.RouteInfoBean
import tkatva.consulting.jmxpoller.CfgHelper
import tkatva.consulting.jmxpoller.JmxPoller
import tkatva.consulting.jmxpoller.smxService
import java.io.File
import scala.collection.mutable.HashMap


class RoutePollerServiceImpl extends smxService.RoutePollerServiceTrait {

  var cfg = new CfgHelper
  var jmxser = new JmxPoller
  var filters : List[String] = null
  init()
  
  private def init() = {
    
    
    
    var filePath = this.getClass.getClassLoader.getResource("cfg.xml").getPath
      //ClassLoader.getSystemResource("cfg.xml").getFile()
    jmxser.srvUrl = cfg.getServicemixUrl(filePath)
    var usrNamePword = cfg.getUsrNameAndPword(filePath)
    jmxser.serviceUsr = usrNamePword._1
    jmxser.servicePword = usrNamePword._2
    jmxser.createConnection
    filters = cfg.getFilters(filePath)
  }
  
  def getRoutesInfo() : List[RouteInfoBean] = {
    
    var routeBeans = scala.collection.mutable.ListBuffer.empty[RouteInfoBean]
    val jBeans = jmxser.getSpecifiedBeans(filters)
    for (jBean <- jBeans) {
      var routeBean : RouteInfoBean = null
      jBean.SubBeans.foreach(jb => routeBean = mapJbeanToRouteBean(jb))
      
     
      routeBean.routeName = routeBean.routeName.replace("\"", "")
       
      routeBeans += routeBean
    }
      
    
    return routeBeans.toList
  }
  
  private def mapJbeanToRouteBean(jBeanParam : JmxBean) : RouteInfoBean = {
    var routeBean = new RouteInfoBean
    routeBean.jFullBeanName = jBeanParam.BeanName
    jBeanParam.BeanName.split(",").foreach(matchStr => if (matchStr.contains("name"))  routeBean.routeName = matchStr.split("=").last.toString )
    //TODO add attributes of the JmxBean to RouteInfo
    routeBean.routeInfoAttributes = jBeanParam.BeanProperties
    return routeBean
  }
  
  private def getStringValues(hmParam : HashMap[String,String]) : HashMap[String,String] = {
    var retHm : HashMap[String,String] = new HashMap[String,String]
    
    for (key <- hmParam.keys) {
      val someValue = retHm.get(key)
      val strVal = someValue match { case Some(s) => s
       
        case None => ""}
      retHm += ((key,strVal))
    }
    
    return retHm
  }  
  def getRefreshRoute(routeInfoParam : RouteInfoBean) : RouteInfoBean = {
    return null;
  }
  
  def loginToSmxPoller(usrName : String, pWord : String) : Boolean = {
    return false
  }
  
}
